package wine.logic.interfaces;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import wine.ejb.utilities.BaseSessionBean;
import wine.ejb.utilities.Constants;
import wine.ejb.utilities.Log;
import wine.logic.bean.WSysUrlDao;
import wine.logic.entity.WSysUrl;

@Stateless
@Remote(WSysUrlDao.class)
public class WSysUrlImpl extends BaseSessionBean<WSysUrl> implements WSysUrlDao {

	@Override
	public WSysUrl countURL(String url) {
		try {
			List<WSysUrl> lst = getWSysUrlDao().findByURL(url);
			if (lst == null || lst.size() == 0) {
				WSysUrl sysUrl = new WSysUrl();
				/* 第一次插入的时候,把group,name,url设置为同一个值 */
				sysUrl.setUrl(url);
				sysUrl.setMdlName(url);
				/* 默认为所有rul都公开,为测试用 */
				sysUrl.setPmsLevel(Constants.PUBLIC_PMS_LEVEL);
				sysUrl.setVisitCnt(1l);
				sysUrl = save(sysUrl);
				return sysUrl;
			} else {
				WSysUrl sysUrl = lst.get(0);
				sysUrl.setVisitCnt(sysUrl.getVisitCnt() + 1);
				return save(sysUrl);
			}
		} catch (Exception e) {
			Log.error(e.getMessage());
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<WSysUrl> findByURL(String URL) {
		return em.createQuery("from WSysUrl u where u.url like :url").setParameter("url", URL).getResultList();
	}

}
